/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewOrder.java
 *
 * Created on 12-dec-2011, 13:26:00
 */
package view;

import java.util.List;
import javax.swing.table.DefaultTableModel;
import main.WinkelApplication;
import model.Order;
import model.OrderLine;

/**
 *
 * @author Bryan
 */
public class ViewOrder extends javax.swing.JPanel {
    private String status;
    private Order order;
    private int orderId;
    private int curStatus;
    private boolean menu;
    private List<OrderLine> orderLines;
    
    /** Creates new form ViewOrder */
    public ViewOrder(int orderId,boolean menu) {
        initComponents();
        this.menu = menu;
        this.orderId = orderId;
        order = WinkelApplication.getQueryManager().getOrder(orderId);
        curStatus = order.getStatus();
        
        lblCustomer.setText(order.getCustomer());
        lblDate.setText(order.getDate());
        lblPaymentmethod.setText(order.getPaymentmethod());
        lblOrderId.setText(String.valueOf(orderId));
        txtNote.setText(order.getNote());
        
        if(curStatus == 0) lblStatus.setText("Openstaand");
        else if(curStatus == 1) lblStatus.setText("Afgerond");
        else if(curStatus == -1) lblStatus.setText("Geannuleerd");
        else lblStatus.setText("Niet-bestaand!");
        
        orderLines = WinkelApplication.getQueryManager().getOrderLines(orderId);
        DefaultTableModel modelOrderProducts = (DefaultTableModel) tblOrderProducts.getModel();
        
        for(OrderLine ol: orderLines) {
            modelOrderProducts.addRow(new Object[] {
                ol.getProductName(),
                ol.getAmount()});
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblOrderId = new javax.swing.JLabel();
        lblDate = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        lblPaymentmethod = new javax.swing.JLabel();
        lblCustomer = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtNote = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblOrderProducts = new javax.swing.JTable();
        btnBack = new javax.swing.JButton();
        btnChangeStatus = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        jLabel1.setText("Klant:");

        jLabel2.setText("Order #:");

        jLabel3.setText("Betaald via:");

        jLabel4.setText("Datum:");

        jLabel5.setText("Status:");

        jLabel6.setText("Opmerking:");

        jLabel7.setText("Producten:");

        lblOrderId.setText("jLabel8");

        lblDate.setText("jLabel9");

        lblStatus.setText("jLabel10");

        lblPaymentmethod.setText("jLabel11");

        lblCustomer.setText("jLabel12");

        txtNote.setColumns(20);
        txtNote.setRows(5);
        txtNote.setEnabled(false);
        jScrollPane1.setViewportView(txtNote);

        tblOrderProducts.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Product", "Aantal"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblOrderProducts);

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgBack.png"))); // NOI18N
        btnBack.setText("Terug");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        btnChangeStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgSelect.gif"))); // NOI18N
        btnChangeStatus.setText("Verander Status");
        btnChangeStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeStatusActionPerformed(evt);
            }
        });

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgDelete.png"))); // NOI18N
        btnCancel.setText("Annuleer");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblOrderId, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                            .addComponent(lblDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(35, 35, 35)
                                .addComponent(lblCustomer, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblPaymentmethod, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))))
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnBack)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnChangeStatus)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblOrderId)
                    .addComponent(jLabel1)
                    .addComponent(lblCustomer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lblDate)
                    .addComponent(jLabel3)
                    .addComponent(lblPaymentmethod))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblStatus))
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBack)
                    .addComponent(btnChangeStatus)
                    .addComponent(btnCancel))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    public void changeStatus(boolean cancel) {
        if(cancel) {
            WinkelApplication.getQueryManager().updateOrderStatus(orderId, -1);
            status = "Geannuleerd";
            curStatus = -1;
        } else {
            if(curStatus == 0) {
                WinkelApplication.getQueryManager().updateOrderStatus(orderId, 1);
                status = "Afgerond";
                curStatus = 1;
            } else if(curStatus == 1) {
                WinkelApplication.getQueryManager().updateOrderStatus(orderId, 0);
                status = "Openstaand";
                curStatus = 0;
            } else if(curStatus == -1) {
                WinkelApplication.getQueryManager().updateOrderStatus(orderId, 0);
                status = "Geannuleerd";
                curStatus = 0;
            } else status = "Niet-bestaand!";
        }
        lblStatus.setText(status);
    }
    
    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        WinkelApplication.getInstance().showPanel(new Orders(this.menu, -1));
    }//GEN-LAST:event_btnBackActionPerformed

    private void btnChangeStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeStatusActionPerformed
        if(curStatus != -1) changeStatus(false);
        else WinkelApplication.msgbox("Deze order is geannuleerd!");
    }//GEN-LAST:event_btnChangeStatusActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        if(curStatus != -1) {
            changeStatus(true);
            WinkelApplication.getQueryManager().cancelOrder(orderId, orderLines);
        } else WinkelApplication.msgbox("Deze order is geannuleerd!");
    }//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnChangeStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCustomer;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblOrderId;
    private javax.swing.JLabel lblPaymentmethod;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JTable tblOrderProducts;
    private javax.swing.JTextArea txtNote;
    // End of variables declaration//GEN-END:variables
}
